package com.etc.demo0530.controller;


import com.etc.demo0530.entity.Goods;
import com.etc.demo0530.service.GoodsServie;
import com.etc.demo0530.util.ResponseData;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("api")
public class GoodsController {

    //针对于Goods进行管理的后端接口 =调用GoodsService
    @Autowired
    private GoodsServie service;


    /**
     *
     * @param pageNum
     * @param pageSize
     * @param keywords
     * @return
     */
    @GetMapping("goods")
    public ResponseData<PageInfo<Goods>> selectByLike(@RequestParam(value = "pageNum",required = true,defaultValue = "1") Integer pageNum, @RequestParam(value = "pageSize",required = true,defaultValue = "5") Integer pageSize, @RequestParam(value = "keywords",required = true,defaultValue = "")  String keywords){

        PageInfo<Goods> pageInfo = service.selectByLike(pageNum,pageSize,keywords);

        ResponseData responseData = new ResponseData();
        responseData.setPageinfo(pageInfo);
        return responseData;
    }


    /**
     *
     * @param goodsid
     * @return
     */
    @GetMapping("goods/{goodsid}")
    public ResponseData<Goods> selectById(@PathVariable("goodsid") Integer goodsid){
        Goods goods = service.selectById(goodsid);
        ResponseData responseData = new ResponseData();
        responseData.setObj(goods);
        return responseData;
    }


    /**
     *
     * @param goodsid
     * @return
     */
    @DeleteMapping("goods/{goodsid}")
    public ResponseData deleteById(@PathVariable("goodsid") Integer goodsid){
        //调用删除业务操作
        int result =  service.deleteById(goodsid);

        ResponseData responseData = new ResponseData();
        if(result == 0){
            responseData.setStatus("error");
        }

        return  responseData;
    }


    /**
     *
     * @param goods
     * @return
     */
    @PostMapping("goods")
    public ResponseData addGoods(@RequestBody Goods goods){
        //调用删除业务操作
        int result =  service.addGoods(goods);

        ResponseData responseData = new ResponseData();
        if(result == 0){
            responseData.setStatus("error");
        }

        return  responseData;
    }
}
